using System;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Pegasus
{
	/// <summary>
	/// Extends the Uri class with some added functionality.
	/// </summary>
	[Serializable]
	[TypeConverter( typeof( UriTypeConverter ) )]
	public class UriEx : Uri
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UriEx"/> class.
		/// </summary>
		/// <param name="uriString">The URI string.</param>
		public UriEx( string uriString )
			: base( uriString )
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UriEx"/> class.
		/// </summary>
		/// <param name="uriString">The URI string.</param>
		/// <param name="uriKind">Kind of the URI.</param>
		public UriEx( string uriString, UriKind uriKind )
			: base( uriString, uriKind )
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UriEx"/> class.
		/// </summary>
		/// <param name="baseUri">The base URI.</param>
		/// <param name="relativeUri">The relative URI.</param>
		public UriEx( Uri baseUri, string relativeUri )
			: base( baseUri, relativeUri )
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UriEx"/> class.
		/// </summary>
		/// <param name="baseUri">The base URI.</param>
		/// <param name="relativeUri">The relative URI.</param>
		public UriEx( Uri baseUri, Uri relativeUri )
			: base( baseUri, relativeUri )
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UriEx"/> class.
		/// </summary>
		/// <param name="serializationInfo">The serialization info.</param>
		/// <param name="streamingContext">The streaming context.</param>
		protected UriEx( SerializationInfo serializationInfo, StreamingContext streamingContext )
			: base( serializationInfo, streamingContext )
		{
		}

		/// <summary>
		/// Replaces the host in the Uri.
		/// </summary>
		/// <param name="newHost">The new host.</param>
		/// <returns></returns>
		public UriEx ReplaceHost( string newHost )
		{
			// Swap out the server in the URL
			return UriEx.ReplaceHost( this, newHost );
		}

		/// <summary>
		/// Replaces the host.
		/// </summary>
		/// <param name="uri">The URI in which to replace the host section.</param>
		/// <param name="newHost">The new host name.</param>
		/// <returns></returns>
		public static UriEx ReplaceHost( Uri uri, string newHost )
		{
			// Swap out the server in the URL
			return new UriEx( string.Format( "{0}://{1}{2}", uri.Scheme, newHost, uri.PathAndQuery ) );
		}
	}
}
